package com.nnnu.wsnackshop.controller.user;

import com.nnnu.wsnackshop.constant.MessageConstants;
import com.nnnu.wsnackshop.constant.StatusCodeConstants;
import com.nnnu.wsnackshop.pojo.dto.TagQueryDTO;
import com.nnnu.wsnackshop.pojo.vo.TagVO;
import com.nnnu.wsnackshop.result.PageResult;
import com.nnnu.wsnackshop.result.Result;
import com.nnnu.wsnackshop.service.ITagsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户端-商品标签查询接口
 * </p>
 *
 * @author zk
 * @since 2025-05-14
 */
@RestController("userTagsController")
@RequestMapping("/api/user/tags")
@Slf4j
@Validated
@Tag(name = "用户端-标签接口")
@RequiredArgsConstructor
public class TagsController {

    private final ITagsService tagsService;

    @Operation(summary = "分页查询标签", description = "分页查询所有启用的标签")
    @GetMapping("/page")
    public Result<PageResult<TagVO>> pageTags(@Valid @ParameterObject TagQueryDTO dto) {
        log.info("[用户端] 分页查询标签：{}", dto);
        // 用户端只查询启用的标签
        dto.setStatus(1);
        PageResult<TagVO> result = tagsService.pageTags(dto);
        return Result.success(result, MessageConstants.SELECT_OK);
    }

    @Operation(summary = "获取标签详情", description = "根据标签ID获取标签详情")
    @GetMapping("/{id}")
    public Result<TagVO> getDetail(@PathVariable @Parameter(description = "标签ID") @Min(1) Long id) {
        log.info("[用户端] 查询标签详情：{}", id);
        TagVO tag = tagsService.getDetail(id);
        if (tag == null || tag.getStatus() != 1) {
            return Result.error(StatusCodeConstants.NOT_FOUND, "标签不存在或已禁用");
        }
        return Result.success(tag, MessageConstants.SELECT_OK);
    }

    @Operation(summary = "获取所有启用标签", description = "获取所有启用的标签列表")
    @GetMapping("/list")
    public Result<List<TagVO>> getAllEnabledTags() {
        log.info("[用户端] 查询所有启用标签");
        List<TagVO> tags = tagsService.getAllEnabledTags();
        return Result.success(tags, MessageConstants.SELECT_OK);
    }

    @Operation(summary = "批量查询标签", description = "根据标签ID列表批量查询标签")
    @GetMapping("/batch")
    public Result<List<TagVO>> batchGetTags(
            @Parameter(description = "标签ID列表", in = ParameterIn.QUERY) @RequestParam List<@Min(1) Long> ids) {
        log.info("[用户端] 批量查询标签：{}", ids);
        List<TagVO> tags = tagsService.batchGetEnabledTags(ids);
        return Result.success(tags, MessageConstants.SELECT_OK);
    }

    @Operation(summary = "搜索标签", description = "根据关键词搜索启用的标签")
    @GetMapping("/search")
    public Result<List<TagVO>> searchTags(
            @Parameter(description = "搜索关键词", in = ParameterIn.QUERY) @RequestParam String keyword) {
        log.info("[用户端] 搜索标签，关键词：{}", keyword);
        List<TagVO> tags = tagsService.searchEnabledTags(keyword);
        return Result.success(tags, MessageConstants.SELECT_OK);
    }

    @Operation(summary = "获取热门标签", description = "获取使用频率最高的标签")
    @GetMapping("/popular")
    public Result<List<TagVO>> getPopularTags(
            @Parameter(description = "返回数量限制", in = ParameterIn.QUERY) @RequestParam(defaultValue = "10") @Min(1) Integer limit) {
        log.info("[用户端] 查询热门标签，限制数量：{}", limit);
        List<TagVO> tags = tagsService.getPopularTags(limit);
        return Result.success(tags, MessageConstants.SELECT_OK);
    }
}
